package com.example.he.p0307_keyevent;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.Toast;

public class KeyDownActivity extends AppCompatActivity {

    // 自定义的Button
    EditText showText1, showText2, showText3, showText4;
    public void onCreate(Bundle savedInstanceState) { // 重写的onCreate方法
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_down);
        showText1 = (EditText) findViewById(R.id.showTxt1);
        showText2 = (EditText) findViewById(R.id.showTxt2);
        showText3 = (EditText) findViewById(R.id.showTxt3);
        showText4 = (EditText) findViewById(R.id.showTxt4);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 重写的键盘按下监听
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                showText1.setText("点击了【回退键】");
                break;
            case KeyEvent.KEYCODE_A:
                showText1.setText("点击了【 A键】");
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                showText1.setText("点击了【 音量-】");
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                showText1.setText("点击了【 音量+】");
                break;
            default:
                break;
        }
        //
        return true;
        //return super.onKeyDown(keyCode, event);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // 重写的键盘按下监听
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                showText2.setText("抬起了【回退键】");
                break;
            case KeyEvent.KEYCODE_A:
                showText2.setText("抬起了【 A键】");
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                showText2.setText("抬起了【 音量-】");
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                showText2.setText("抬起了【 音量+】");
                break;
            default:
                break;
        }
        //
        return true;
        //return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // 重写的键盘按下监听
        Toast.makeText(this, "longPress", Toast.LENGTH_LONG).show();
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                showText3.setText("长按了【回退键】");
                break;
            case KeyEvent.KEYCODE_A:
                showText3.setText("长按了【 A键】");
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                showText3.setText("长按了【 音量-】");
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                showText3.setText("长按了【 音量+】");
                break;
            default:
                break;
        }
        //
        return true;
        //return super.onKeyLongPress(keyCode, event);
    }

    public boolean onTouchEvent(MotionEvent event) {
        // 重写的触摸监听
        showText4.setText("Touch-- X:"+ event.getX() + "  Y:" + event.getY());
        return true;
        //return super.onTouchEvent(keyCode, event);
    }
}
